//app.js
App({
  setUserInfo: function (res) {
    // 可以将 res 发送给后台解码出 unionId
    let userInfo = res.userInfo;
    this.globalData.userInfo = { ...this.globalData.userInfo, ...userInfo }

    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    // 所以此处加入 callback 以防止这种情况
    if (this.userInfoReadyCallback) {
      this.userInfoReadyCallback(res)
    }
  },
  saveWechatUserInfo: function ($this, fun) {
    if ($this.globalData.userInfo.id === undefined) {

      // 登录
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          let code = res.code;
          this.globalData.userInfo = { ...this.globalData.userInfo, code }
          wx.request({
            url: $this.globalData.apiUrl + 'videoData/saveWechatUserInfo',
            method: 'POST',
            data: $this.globalData.userInfo,
            success: function (res) {
              let data = res.data.data;
              let ress = {};
              ress.userInfo = { ...data, ...$this.globalData.userInfo }
              $this.setUserInfo(ress);
              console.log('ggggggggggg')
              console.log(ress.userInfo)
              if (typeof fun === 'function') {
                fun($this.globalData.userInfo);
              }

              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })

        }
      })


    } else {
      console.log('yyyyyyyyyyyyyy')
      if (typeof fun === 'function') {
        fun($this.globalData.userInfo);
      }
    }

  },
  onLaunch: function () {
    console.log('bbbbbbbbbbbbbb')
    var $this = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 获取用户信息
    // wx.getSetting({
      // success: res => {
        //  if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setUserInfo(res);
            }, fail: function () {
              wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
                success: function (res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success: (res) => {
                        if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                          wx.getUserInfo({
                            success: function (res) {
                              this.setUserInfo(res);
                            }
                          })
                        }
                      }, fail: function (res) {
                      }
                    })
                  }
                }
              })
            }, complete: function (res) {
            }
          })
        // }
      // }
    // })
  },
  globalData: {
    userInfo: {},
    apiUrl: "https://api.hslh.site/",
    // apiUrl: "http://localhost:8580/teachingSys/",
  }
})
